// pages/index/image.js
Component({
  data: {
    page: 1,
    picList: [
      {
        src: "/static/image/pic/baoyu.jpg"
      },
      {
        src: "/static/image/pic/dayu.jpg"
      },
      {
        src: "/static/image/pic/default.jpg"
      },
      {
        src: "/static/image/pic/duoyun.jpg"
      },
      {
        src: "/static/image/pic/baoyu.jpg"
      },
      {
        src: "/static/image/pic/leizhenyu.jpg"
      },
      {
        src: "/static/image/pic/qing.jpg"
      },
      {
        src: "/static/image/pic/wu.jpg"
      },
      {
        src: "/static/image/pic/xiaoxue.jpg"
      },
      {
        src: "/static/image/pic/xiaoyu.jpg"
      }
    ],
    page: 1,
    isMoveToUp: true,
  },
  methods: {
    preview(e) {
      console.log(e)
      let urls = [e.target.dataset.url]
      wx.previewImage({
        urls: urls
      })
    },
    loadMore() {
      console.log("我在加载")
      ++this.data.page
      this.retrieve()
    },
    async retrieve() {
      wx.showLoading({
        title: '加载中',
      });
      
      let tempArr = []
      let url = `https://autumnfish.cn/api/joke`
      for(let i=0; i<10; i++) {
        let res = await new Promise(function(resolve, reject) {
          wx.request({
            url: url,
            success(res) {
              resolve(res)
            },
            fail(err) {
              reject(err)
            },
            complete() {
              wx.hideLoading()
            }
          })
        })
  
        tempArr.push({
          id: "text_" + i,
          title: res.data.substr(0, 4),
          content: res.data.replace(/<br\/>/g, '\n')
        })
      }
  
      let picTempArr = this.data.picList.map((item, index, arr) => {
        return {
          id: tempArr[index].id,
          title: tempArr[index].title,
          content: tempArr[index].content,
          src: item.src
        }
      })
  
      this.setData({
        jokeList: tempArr,
        picList: picTempArr
      })
      
    }
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
      this.retrieve()
    }
  }
})
